<?php

class Silk_Pms_Block_Adminhtml_Activity_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
  public function __construct()
  {
      parent::__construct();
      $this->setId('activityGrid');
      $this->setDefaultSort('id');
      $this->setDefaultDir('DESC');
      $this->setSaveParametersInSession(true);
  }


  protected function _prepareCollection()
  {
      $collection = Mage::getModel('pms/activity')->getCollection();
      $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
      if($websiteId){
          $collection->addFieldToFilter("website_id",$websiteId);
      }
      $this->setCollection($collection);
      return parent::_prepareCollection();
  }


  protected function _prepareColumns()
  {
      $this->addColumn('id', array(
            'header'    => Mage::helper('pms')->__('Id'),
            'align'     => 'center',
            'width'     => '80px',
            'index'     => 'id',
      ));

      $this->addColumn('name', array(
          'header'    => Mage::helper('pms')->__('活动名称'),
          'align'     => 'center',
          'width'     => '80px',
          'index'     => 'name',
      ));

      $this->addColumn('website_id', array(
          'header'    => Mage::helper('pms')->__('Website'),
          'align'     => 'center',
          'width'     => '80px',
          'type'      => 'options',
          'options'   => $this->getWebsite(),
          'index'     => 'website_id',
      ));

//      $this->addColumn('admin_id', array(
//            'header'    => Mage::helper('pms')->__('操作员ID'),
//            'align'     => 'right',
//            'index'     => 'admin_id',
//      ));

      $this->addColumn('start_at', array(
            'header'    => Mage::helper('pms')->__('开始时间'),
            'align'     => 'center',
            'index'     => 'start_at',
            'type'      => 'date',
            'format'    => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
      ));

      $this->addColumn('end_at', array(
            'header'    => Mage::helper('pms')->__('结束时间'),
            'align'     => 'center',
            'index'     => 'end_at',
            'type'      => 'date',
            'format'    => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
      ));

//      $this->addColumn('description', array(
//            'header'    => Mage::helper('pms')->__('描述'),
//            'align'     => 'left',
//            'index'     => 'description',
//      ));

//      $this->addColumn('created_at', array(
//            'header'    => Mage::helper('pms')->__('创建时间'),
//            'align'     => 'left',
//            'index'     => 'created_at',
//            'type'      => 'date',
//            'format'    => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
//      ));

      $status = array('0'=>'禁用','1'=>'启用');
      $this->addColumn('status', array(
              'header'    => Mage::helper('pms')->__('状态'),
              'align'     => 'center',
              'width'     => '100',
              'index'     => 'status',
              'type'      => 'options',
              'options'   => $status
      ));

      $template = array(
          '1'=>'五一模板--PC版',
          '2'=>'五一模板--移动版',
          '3'=>'端午模板--PC版',
          '4'=>'端午模板--移动版',
          '5'=>'中秋模板--PC版',
          '6'=>'中秋模板--移动版',
          '7'=>'国庆模板--PC版',
          '8'=>'国庆模板--移动版'
      );
      $this->addColumn('template_id', array(
              'header'    => Mage::helper('pms')->__('模板ID'),
              'align'     => 'center',
              'width'     => '150',
              'index'     => 'template_id',
              'type'      => 'options',
              'options'   => $template
      ));

      $this->addColumn('action', array(
            'header'    => Mage::helper('pms')->__('Action'),
            'width'     => '100',
            'align'     => 'center',
            'type'      => 'action',
            'getter'    => 'getId',
            'actions'   =>  array(
                array(
                    'caption'   => Mage::helper('pms')->__('编辑'),
                    'url'       => array('base'=> '*/*/edit'),
                    'field'     => 'id'
                ),
            ),
            'filter'    => false,
            'sortable'  => false,
            'index'     => 'stores',
            'is_system' => true,
      ));

      $this->addColumn('page_actions', array(
          'header'    => Mage::helper('cms')->__('预览'),
          'width'     => 100,
          'align'     => 'center',
          'sortable'  => false,
          'filter'    => false,
          'renderer'  => 'pms/adminhtml_activity_renderer_action',
      ));

      return parent::_prepareColumns();
  }


  protected function _prepareMassaction()
  {
        $this->setMassactionIdField('id');
        $this->getMassactionBlock()->setFormFieldName('pms');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('pms')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('pms')->__('Are you sure?')
        ));
        $statuses = array("0"=>"禁用","1"=>"启用");
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('catalog')->__('Change status'),
             'url'  => $this->getUrl('*/*/massChangeStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('pms')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));
        return $this;
  }


  public function getRowUrl($row)
  {
      return $this->getUrl('*/*/edit', array('id' => $row->getId()));
  }

  protected function getWebsite()
  {
        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
        $website = Mage::getModel('core/website')->getCollection()->toOptionHash();
        $outs= array();
        if (key_exists($websiteId,$website))
        {
            $websitename = $website[$websiteId];
            $outs[$websiteId]=$websitename;
            return $outs;
        }
        return $website;
  }

}